package com.luoy.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@RestController
public class FileUpload {
    public static String upFile(MultipartFile srcFile, HttpSession session) {
        //获得上传文件的名字
        String filename = srcFile.getOriginalFilename();
        String type = srcFile.getContentType();

        //将上传的文件保存到服务器
        String realPath = session.getServletContext().getRealPath("/img");
        //将上传的文件保存到服务器的指定位置
        try {
            srcFile.transferTo(new File(realPath + "/" + filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filename;
    }
}
